package com.commpany.servie.basic.promotion.service.read;

import com.alibaba.dubbo.rpc.RpcContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.*;
import java.util.stream.Collectors;

import com.commpany.servie.basic.promotion.api.read.ProductPromotionMapReadService;
import com.commpany.jdq.core.common.PageResult;
import com.commpany.jdq.core.orm.Pagination;
import com.commpany.servie.basic.promotion.business.read.productPromotionMap.ProductPromotionMapReadManage;
import com.commpany.servie.basic.promotion.model.dto.productpromotionmap.ProductPromotionMapDTO;
import com.commpany.servie.basic.promotion.converter.ProductPromotionMapConverter;
import com.commpany.servie.basic.promotion.po.ProductPromotionMapPO;
import com.commpany.servie.basic.promotion.model.dto.PageResultDTO;
@Service("productPromotionMapReadService")
public class ProductPromotionMapReadServiceImpl  implements ProductPromotionMapReadService {
	@Autowired
	private ProductPromotionMapReadManage productPromotionMapReadManage;
	
	public ProductPromotionMapDTO getProductPromotionMapService(ProductPromotionMapDTO inputDTO){
	
	   return ProductPromotionMapConverter.toDTO(productPromotionMapReadManage.getProductPromotionMapService(ProductPromotionMapConverter.toPO(inputDTO)));
	}
   @Override
	public PageResultDTO<ProductPromotionMapDTO> selectProductPromotionMapPageService(ProductPromotionMapDTO inputDTO){
	   Map<String, String> attachments = RpcContext.getContext().getAttachments();

	   ProductPromotionMapPO po = ProductPromotionMapConverter.toPO(inputDTO);
		Pagination page = new Pagination();
	    page.setLimitStart(inputDTO.getLimitStart());
		page.setPageNo(inputDTO.getPageNo());
		page.setPageSize(inputDTO.getPageSize());
		Optional.ofNullable(inputDTO.getOrderBy()).ifPresent(page::setOrderBy);
		PageResult<ProductPromotionMapPO> poPage = productPromotionMapReadManage.selectProductPromotionMapPageService( po,page);
		List<ProductPromotionMapPO> poList = poPage.getList();
		if(Objects.isNull(poList)) {
	    	 return PageResultDTO.<ProductPromotionMapDTO>builder().build();
	     }		
		 List<ProductPromotionMapDTO> dtoList = poList.stream().map(src-> ProductPromotionMapConverter.toDTO(src)).collect(Collectors.toList());
		return 	PageResultDTO.<ProductPromotionMapDTO>builder().pageNo(poPage.getPageNo()).pageSize(poPage.getPageSize()).list(dtoList).build();
	}
   @Override
    public int countProductPromotionMapService( ProductPromotionMapDTO inputDTO ){
         ProductPromotionMapPO po = ProductPromotionMapConverter.toPO(inputDTO);
         return productPromotionMapReadManage.countProductPromotionMapService(po);
    }
     @Override
	public List<ProductPromotionMapDTO> queryProductPromotionMapService (ProductPromotionMapDTO inputDTO){
	    ProductPromotionMapPO po = ProductPromotionMapConverter.toPO(inputDTO);
	     List<ProductPromotionMapPO> list = productPromotionMapReadManage.queryProductPromotionMapService(po);
		   if(Objects.isNull(list)) {
	    	 return new ArrayList<ProductPromotionMapDTO>();
	     }
		return list.stream().map(src-> ProductPromotionMapConverter.toDTO(src)).collect(Collectors.toList());
	}
}
	